/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ia.tpfinal;

import ia.tpfinal.jmetal.base.*;
import ia.tpfinal.jmetal.base.operator.crossover.CrossoverFactory;
import ia.tpfinal.jmetal.base.operator.mutation.MutationFactory;
import ia.tpfinal.jmetal.base.operator.selection.SelectionFactory;
import ia.tpfinal.jmetal.metaheuristics.nsgaII.NSGAII;
import ia.tpfinal.jmetal.problems.BiObjective.TSP;
import ia.tpfinal.jmetal.qualityIndicator.QualityIndicator;
import ia.tpfinal.jmetal.util.JMException;
import ia.tpfinal.jmetal.util.Ranking;
import ia.tpfinal.moacofinal.M3AS;
import ia.tpfinal.moacofinal.MOACO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

/**
 *
 * @author Verena
 */
public class IaTpfinal {

    /**
     * @param args the command line arguments
     */
    public static Logger logger_;      // Logger object
    public static FileHandler fileHandler_; // FileHandler object

    public static void main(String[] args) throws JMException, SecurityException, IOException {

       
        String[] problemasTSP = {"kroac100.tsp.txt", "KROAB100.TSP.TXT"};
        String pathNameFront = "src/ia/tpfinal/files/tsp/Front";
        String problema = "";
        String name = "";
        SolutionSet soluciones = new SolutionSet(2000);
        for (int e = 0; e < 2; e++) {
            String[] frontPath = new String[10];
            name = problemasTSP[e];
            problema = name.substring(0, 5) + "-";

            for (int i = 0; i < 5; i++) {

                Problem problem;         // The problem to solve
                Algorithm algorithm;         // The algorithm to use
                Operator crossover;         // Crossover operator
                Operator mutation;         // Mutation operator
                Operator selection;         // Selection operator

                String problemName = "src/ia/tpfinal/files/tsp/";
                QualityIndicator indicators; // Object to get quality indicators

                indicators = null;
                // Default problem
                problem = new TSP(problemName + name);
                algorithm = new NSGAII(problem);

                // Algorithm parameters
                algorithm.setInputParameter("populationSize", 100);
                algorithm.setInputParameter("maxEvaluations", 25000);

                // Mutation and Crossover for Real codification
                crossover = CrossoverFactory.getCrossoverOperator("TwoPointsCrossover");
                crossover.setParameter("probability", 0.9);
                crossover.setParameter("distributionIndex", 20.0);

                mutation = MutationFactory.getMutationOperator("SwapMutation");
                mutation.setParameter("probability", 1.0 / problem.getNumberOfVariables());
                mutation.setParameter("distributionIndex", 20.0);

                // Selection Operator
                selection = SelectionFactory.getSelectionOperator("BinaryTournament2");

                // Add the operators to the algorithm
                algorithm.addOperator("crossover", crossover);
                algorithm.addOperator("mutation", mutation);
                algorithm.addOperator("selection", selection);

                // Add the indicator object to the algorithm
                algorithm.setInputParameter("indicators", indicators);

                // Execute the Algorithm
                SolutionSet population = algorithm.execute();

                Ranking ranking = new Ranking(population);
                SolutionSet bestFront = ranking.getSubfront(0);
                for (int k = 0; k < bestFront.size(); k++) {

                    soluciones.add(bestFront.get(k));
                }


                // bestFront.printVariablesToFile("src/ia/tpfinal/files/tsp/VAR-" + i + ".txt");

                bestFront.printObjectivesToFile(pathNameFront + problema + i + ".txt");

                frontPath[i] = pathNameFront + problema + i + ".txt";


            }//end for

            Util u = new Util();
            String parametros = new String(new char[100]);
            String archivoParametros = "parametros_tsp.txt";
            parametros = "src/ia/tpfinal/files/tsp/" + archivoParametros;
            MOACO alg; // instancia del algoritmo
            ia.tpfinal.moacofinal.Problem prob;

            for (int i = 5; i < 10; i++) {
                prob = new ia.tpfinal.moacofinal.TSP("src/ia/tpfinal/files/tsp/" + problemasTSP[e]);
                alg = new M3AS(prob, parametros);
                alg.ejecutarTSP();
                SolutionSet bestFront = u.convertirPareto(prob, alg.pareto);
                bestFront.printObjectivesToFile(pathNameFront + problema + i + ".txt");
                frontPath[i] = pathNameFront + problema + i + ".txt";
                for (int k = 0; k < bestFront.size(); k++) {
                    soluciones.add(bestFront.get(k));
                }

                // alg.pareto.agregarSoluciones(prob, cad);
            }
            System.out.println("Tamanio de soluciones:" + soluciones.size());
            Ranking rank = new Ranking(soluciones);
            SolutionSet Ytrue = rank.getSubfront(0);
            System.out.println("Tamanio de Ytrue:" + Ytrue.size());
            Ytrue.printObjectivesToFile("src/ia/tpfinal/files/tsp/yTrue"+problema.substring(0,problema.length()-1)+".txt");

            // Metricas

            List<Double[]> frenteActual;
            List<Double[]> YtrueM = u.getDatosFromArchivo("src/ia/tpfinal/files/tsp/yTrue"+problema.substring(0,problema.length()-1)+".txt"); // el que decimos Ytrue
            u.calcularMetricas("src/ia/tpfinal/files/tsp/metricas"+problema.substring(0,problema.length()-1) +".txt", YtrueM, frontPath, problema);
        }
    }
}
